package pl.lodz.p.kipme.entities;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import pl.lodz.p.kipme.bo.Order;
import pl.lodz.p.kipme.bo.ProductOrder;


@Table(name = "ORDER_ENTITIES", uniqueConstraints = { @UniqueConstraint(columnNames = "ORDER_ID") })
@Entity
public class OrderEntity {
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;
	@Column(name="ORDER_ID")
	private String orderId;
	private Date date;
	@OneToMany(cascade = CascadeType.ALL, mappedBy="order")
	private List<ProductOrderEntity> products;

	public OrderEntity() {
	}

	public OrderEntity(Order order) {
		this.date = order.getDate();
		this.orderId = order.getOrderId();
		products = new ArrayList<ProductOrderEntity>();
		for (ProductOrder productOrder : order.getProducts()) {
			products.add(new ProductOrderEntity(productOrder, this));
		}
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getOrderId() {
		return orderId;
	}

	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public List<ProductOrderEntity> getProducts() {
		return products;
	}

	public void setProducts(List<ProductOrderEntity> products) {
		this.products = products;
	}

}
